package com.attack.manager;

import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Service;

import com.attack.model.ReportSourceModel;
import com.attack.model.ReportTableModel;
import com.attack.model.ReportTargetModel;
import com.common.manager.BaseManager;
import com.common.utils.DateUtil;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;

@Service(value = "reportManager")
public class ReportManager  extends BaseManager{
	
	@Override
	public void setSqlMapNameSpace() {
		this.sqlMapNameSpace = "com.common.system.model.report";
	}
	
	public void generateReport(int tableId){
		IData dataMap = new DataMap();
		dataMap.put("tableId", tableId);
		//获取表信息
		ReportTableModel reportTableModel = (ReportTableModel)super.getObjectInfo("getTablesObject",dataMap);
		
		//获取列信息
		IData columnData  = getTableColumns(reportTableModel);
		
		//第一步：先清除执行的表
		this.dropTable(reportTableModel);
		
		dataMap.put("columnName","");
		
		//第二步：生成表
		this.createTable(reportTableModel,String.valueOf(columnData.get("column")));
		
		//第三步：生成临时表数据
		this.generateTempData(reportTableModel);
		
		//第四步：生成结果表数据
		this.generateResultData(reportTableModel);
		
		//第五步：搬迁数据到正式表
	}
	
	
	/**
	 * 生成动态的Y表和日期表
	 * @param tableId
	 */
	public void createTable(ReportTableModel reportTableModel,String columnNames){
		StringBuffer sqlBuffer = new StringBuffer("");
		sqlBuffer.append("CREATE TABLE ").append(reportTableModel.getTableNameY()).append("(").append(columnNames).append(")");
		log.debug("创建临时表SQL:="+sqlBuffer.toString());
		IData dataMap = new DataMap();
		dataMap.put("execSql", sqlBuffer.toString());
		super.updateObject("execSql",dataMap);
		//创建周期表
		sqlBuffer = new StringBuffer("");
		sqlBuffer.append("CREATE TABLE ").append(getTableName(reportTableModel)).append("(").append(columnNames).append(")");
		log.debug("创建周期表SQL:="+sqlBuffer.toString());
		dataMap.put("execSql", sqlBuffer.toString());
		super.updateObject("execSql",dataMap);
		
		sqlBuffer = new StringBuffer("");
		sqlBuffer.append("CREATE TABLE ").append(reportTableModel.getTableName()).append("(").append(columnNames).append(")");
		log.debug("生成正式表SQL:="+sqlBuffer.toString());
		dataMap.put("execSql", sqlBuffer.toString());
		super.updateObject("execSql",dataMap);
	}
	
	
	/**
	 * 功能：执行表的删除操作
	 * @param reportTableModel
	 */
	public void dropTable(ReportTableModel reportTableModel){
		IData dataMap = new DataMap();
		StringBuffer sqlBuffer = new StringBuffer("");
		try{
			sqlBuffer.append("DROP TABLE ").append(reportTableModel.getTableNameY());
			log.debug("创建临时表SQL:="+sqlBuffer.toString());
			dataMap.put("execSql", sqlBuffer.toString());
			super.updateObject("execSql",dataMap);
		}catch(Exception e){
			log.info("执行表的清除，表不存在");
		}
		try{
			//创建正式表
			sqlBuffer = new StringBuffer("");
			sqlBuffer.append("DROP TABLE ").append(reportTableModel.getTableName());
			log.debug("创建临时表SQL:="+sqlBuffer.toString());
			dataMap.put("execSql", sqlBuffer.toString());
			super.updateObject("execSql",dataMap);
		}catch(Exception e){
			log.info("执行表的清除，表不存在");
		}
	}
	
	
	
	/**
	 * 功能：生成统计的结果数据
	 * @param reportTableModel
	 */
	public void generateResultData(ReportTableModel reportTableModel){
		IData dataMap = new DataMap();
		StringBuffer sqlBuffer = new StringBuffer("");
		sqlBuffer.append("INSERT INTO ").append(reportTableModel.getTableName()).append(reportTableModel.getGroupSQL().replaceAll("$TABLE_Y", reportTableModel.getTableNameY()));
		log.debug("生成结果信息:="+sqlBuffer.toString());
		dataMap.put("execSql", sqlBuffer.toString());
		super.updateObject("execSql",dataMap);
	}
	
	
	/**
	 * 功能：生成统计的临时数据
	 * @param reportTableModel
	 */
	public void generateTempData(ReportTableModel reportTableModel){
		IData dataMap = new DataMap();
		dataMap.put("tableId", reportTableModel.getTableId());
		List<ReportSourceModel> sourceList = super.getObjectList("getSourceList",dataMap);
		for (ReportSourceModel reportSourceModel : sourceList) {
			StringBuffer sqlBuffer = new StringBuffer("");
			sqlBuffer.append("INSERT INTO ").append(reportTableModel.getTableNameY()).append(reportSourceModel.getParamSql());
			log.debug("生成结果信息:="+sqlBuffer.toString());
			dataMap.put("execSql", sqlBuffer.toString());
			super.updateObject("execSql",dataMap);
		}
	}
	
	
	/**
	 * 功能：搬迁数据
	 * @param reportTableModel
	 */
	public void moveData(ReportTableModel reportTableModel,String columnNames){
		int tableid = reportTableModel.getTableId();
		String execTime = getExecTime(reportTableModel);
		IData dataMap = new DataMap();
		StringBuffer sqlBuffer = new StringBuffer("");
		sqlBuffer.append("DELETE FROM  ").append(reportTableModel.getTableName()).append(" WHERE table_id="+tableid +" and execCycle='"+execTime+"'");
		log.debug("删除结果表中数据:="+sqlBuffer.toString());
		dataMap.put("execSql", sqlBuffer.toString());
		super.updateObject("execSql",dataMap);
		
		sqlBuffer = new StringBuffer("");
		sqlBuffer.append("INSERT INTO ").append(reportTableModel.getTableName()).append("(").append(columnNames+",table_id,execCycle").append(") SELECT ").append(columnNames).append(","+tableid+",'"+execTime+"'").append(" FROM ").append(getTableName(reportTableModel));
		log.debug("生成结果信息:="+sqlBuffer.toString());
		dataMap.put("execSql", sqlBuffer.toString());
		super.updateObject("execSql",dataMap);
	}
	
	
	/**
	 * 功能：根据表ID，查询对应的列信息
	 * @param tableId
	 * @return
	 */
	public IData getTableColumns(ReportTableModel reportTableModel){
		IData dataMap = new DataMap();
		dataMap.put("tableId", reportTableModel.getTableId());
		List<ReportTargetModel> dataList = super.getObjectList("getTableColumns",dataMap);
		StringBuffer columnBuffer = new StringBuffer("");
		StringBuffer columnNameBuffer = new StringBuffer("");
		for (ReportTargetModel reportTargetModel : dataList) {
			columnBuffer.append(reportTargetModel.getParamName()).append(" ").append(reportTargetModel.getParamType()).append(",");
			columnNameBuffer.append(reportTargetModel.getParamName()).append(",");
		}
		if(columnBuffer.length()>1){
			dataMap.put("column", columnBuffer.substring(0, columnBuffer.length()-1));
			dataMap.put("columnName", columnNameBuffer.substring(0, columnNameBuffer.length()-1));
		}else{
			dataMap.put("column","");
			dataMap.put("columnName","");
		}
		return dataMap;
	}

	
	public String getTableName(ReportTableModel reportTableModel){
		if("M".equals(reportTableModel.getExecCycle())){
			return reportTableModel.getTableName()+"_"+getExecTime(reportTableModel);
		}else{
			return reportTableModel.getTableName()+"_"+ getExecTime(reportTableModel);
		}
	}
	
	
	public String getExecTime(ReportTableModel reportTableModel){
		if("M".equals(reportTableModel.getExecCycle())){
			return DateUtil.getTableMonth(reportTableModel.getIntDays());
		}else{
			return DateUtil.getReportDays(reportTableModel.getIntDays());
		}
	}

	public String statOrgOrder() {
		// TODO Auto-generated method stub
		return null;
	}
	
    
}
